package com.shadt.cloud.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginHandler implements HandlerInterceptor{

	/**
	 * Handler执行完成之后调用这个方法 
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	/**
	 *  Handler执行之后，ModelAndView返回之前调用这个方法 
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}

	/**
	 *  Handler执行之前调用这个方法 
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String url = request.getRequestURI();
		String username = (String)session.getAttribute("username");
		if(url.indexOf("login_user")>0){
			return true;
		}
		if(username==null){
			request.getRequestDispatcher("../qczlm/jsp/login.jsp").forward(request, response);
			return false;
		}else{
			return true;
		}
	}

}
